﻿using QYmall.Core.Common.Emum;
using QYmall.Core.Common.Extensions;
using QYmall.Core.DBFactory.Database;
using QYmall.Core.IServices;
using QYmall.Core.Model.Models;
using QYmall.Core.Model.ViewModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace QYmall.Core.Services
{
    /// <summary>
    /// 商户菜单接口实现
    /// </summary>
    public class MerchantMenuService : BaseService, IMerchantMenuService
    {
        private readonly IUserService _userService;
        public MerchantMenuService(IDbContextFactory contentFactory, IUserService userService) : base(contentFactory)
        {
            _userService = userService;
        }

        /// <summary>
        /// 返回所有菜单目录树
        /// </summary>
        public async Task<IEnumerable<MerchantMenuDto>> QueryListAsync(int parentId, WriteRoRead writeAndRead = WriteRoRead.Read)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库

            var oldList = await _context.Set<MerchantMenu>().ToListAsync();
            //调用递归重新生成目录树
            List<MerchantMenuDto> result = await GetChilds(oldList, parentId);
            return result;
        }

        /// <summary>
        /// 返回前台所有菜单目录树
        /// </summary>
        public async Task<IEnumerable<MerchantMenuClientDto>> QueryListAsync(WriteRoRead writeAndRead = WriteRoRead.Read)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库

            var oldList = await _context.Set<MerchantMenu>().ToListAsync();
            //调用递归重新生成目录树
            List<MerchantMenuClientDto> result = await GetClientChilds(oldList, 0);
            return result;
        }

        /// <summary>
        /// 根据条件删除数据(迭代删除)
        /// </summary>
        public async Task<bool> DeleteAsync(Expression<Func<MerchantMenu, bool>> funcWhere, WriteRoRead writeAndRead = WriteRoRead.Write)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库

            var listData = await _context.Set<MerchantMenu>().ToListAsync();//查询所有数据
            var list = await _context.Set<MerchantMenu>().Where(funcWhere).ToListAsync();
            if (list == null)
            {
                return false;
            }
            foreach (var modelt in list)
            {
                await DeleteChilds(listData, modelt.Id);//删除子节点
                _context.RemoveRange(modelt);//删除当前节点
            }
            return await this.SaveAsync();
        }

        #region 辅助私有方法
        /// <summary>
        /// 迭代循环删除
        /// </summary>
        private async Task DeleteChilds(IEnumerable<MerchantMenu> listData, int parentId)
        {
            IEnumerable<MerchantMenu> models = listData.Where(x => x.ParentId == parentId);
            foreach (var modelt in models)
            {
                await DeleteChilds(listData, modelt.Id);//迭代
                _context?.RemoveRange(modelt);
            }
        }

        /// <summary>
        /// 迭代循环目录树(私有方法)
        /// </summary>
        private async Task<List<MerchantMenuDto>> GetChilds(IEnumerable<MerchantMenu> oldList, int parentId)
        {
            List<MerchantMenuDto> listDto = new();
            IEnumerable<MerchantMenu> models = oldList.Where(x => x.ParentId == parentId).OrderByBatch("SortId");//查找并排序
            foreach (var modelt in models)
            {
                MerchantMenuDto modelDto = new()
                {
                    Id = modelt.Id,
                    ParentId = modelt.ParentId,
                    Name = modelt.Name,
                    Title = modelt.Title,
                    SubTitle = modelt.SubTitle,
                    IconUrl = modelt.IconUrl,
                    LinkUrl = modelt.LinkUrl,
                    SortId = modelt.SortId,
                    Status = modelt.Status,
                    Remark = modelt.Remark,
                };
                modelDto.Children.AddRange(
                    await GetChilds(oldList, modelt.Id)
                );
                listDto.Add(modelDto);
            }
            return listDto;
        }

        /// <summary>
        /// 判断权限迭代循环目录树(私有方法)
        /// </summary>
        private async Task<List<MerchantMenuClientDto>> GetClientChilds(IEnumerable<MerchantMenu> oldList, int parentId)
        {
            List<MerchantMenuClientDto> listDto = new();
            IEnumerable<MerchantMenu> models = oldList.Where(x => x.Status == 0 && x.ParentId == parentId).OrderByBatch("SortId");//查找并排序
            foreach (var modelt in models)
            {
                MerchantMenuClientDto modelDto = new()
                {
                    Id = modelt.Id,
                    ParentId = modelt.ParentId,
                    Name = modelt.Name,
                    Text = modelt.Title,
                    Icon = modelt.IconUrl,
                    Href = modelt.LinkUrl,
                };
                modelDto.Children.AddRange(
                    await GetClientChilds(oldList, modelt.Id)
                );
                listDto.Add(modelDto);
            }
            return listDto;
        }
        #endregion
    }
}
